package it.bsoft.gameboy.memory;

import it.bsoft.bin.util.BitMask;
import it.bsoft.bin.util.UByte;

public class FlagRegister extends ByteRegister {

	/* HAS_FLAGS */
	
	private boolean hasFlag(long flagMask){
		return ((flagMask&this.getValue().shortValue()) == flagMask);
	}
	
	public boolean hasZeroFlag(){
		return hasFlag(BitMask.ZERO_FLAG);
	}
	
	public boolean hasSubtractFlag(){
		return hasFlag(BitMask.SUBTRACT_FLAG);
	}
	
	public boolean hasHalfCarryFlag(){
		return hasFlag(BitMask.HALF_CARRY_FLAG);
	}
	
	public boolean hasCarryFlag(){
		return hasFlag(BitMask.CARRY_FLAG);
	}
	
	
	/* SET FLAGS */
	private void setFlag(long flag){
		UByte _flag = UByte.fromLong(flag);
		this.setValue(this.getValue().or(_flag));
	}
	
	public void setZeroFlag(){
		this.setFlag(BitMask.ZERO_FLAG);
	}
	
	public void setSubtractFlag(){
		this.setFlag(BitMask.SUBTRACT_FLAG);
	}
	
	public void setHalfCarryFlag(){
		this.setFlag(BitMask.HALF_CARRY_FLAG);
	}
	
	public void setCarryFlag(){
		this.setFlag(BitMask.CARRY_FLAG);
	}
	
	/* UNSET FLAG */
	
	private void unsetFlag(long flagMask){
		long rMask = ~flagMask;
		UByte _flag = UByte.fromLong(rMask);
		this.setValue(this.getValue().and(_flag));
	}
	
	public void unsetZeroFlag(){
		this.unsetFlag(BitMask.ZERO_FLAG);
	}
	
	public void unsetSubtractFlag(){
		this.unsetFlag(BitMask.SUBTRACT_FLAG);
	}
	
	public void unsetHalfCarryFlag(){
		this.unsetFlag(BitMask.HALF_CARRY_FLAG);
	}
	
	public void unsetCarryFlag(){
		this.unsetFlag(BitMask.CARRY_FLAG);
	}
	
}
